<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Cities;
use App\Models\FilmTypeMaster;
use App\Models\MacroManagement;
use App\Models\MakeMaster;
use App\Models\ModelMaster;
use App\Models\PartMaster;
use App\Models\ProvinceMaster;
use App\Models\RegionMaster;
use Illuminate\Http\Request;

class SettingController extends Controller
{
    protected $view_prefix = 'admin.setting';
    protected $view_cities_index = 'admin.setting.view_cities_index';
    protected $view_index = 'admin.setting.index';
    protected $view_edit = 'admin.setting.edit';

    protected $get = [
        'cities' => '',
    ];
    protected $delete = [
    ];
    protected $put = [
    ];
    protected $post = [
    ];

    public function index(Request $request)
    {
        return ['status' => false, 'msg'=>'尚未登录'];
    }

    /*public function view_cities(Request $request)
    {
        $page = [
            'page_name' => 'cities',
            'title' => ts('CITY'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = $this->view_list($request, 'cities');
        return view($this->view_index, [
            'data'=>$data,
            'page' => $page,
        ]);
    }

    public function edit_cities(Request $request, $id)
    {
        $page = [
            'page_name' => 'cities',
            'title' => ts('CITY'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = Cities::findOrFail($id);
        return view($this->view_edit, [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function update_cities(Request $request, $id)
    {
        $page = [
            'page_name' => 'cities',
            'title' => ts('CITY'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $model = Cities::findOrFail($id);
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        return redirect('/admin/setting/cities')->with('trash', 'sad');
    }

    public function view_province(Request $request)
    {
        $page = [
            'page_name' => 'province',
            'title' => ts('PROVINCE'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = $this->view_list($request, 'province');
        return view($this->view_index, [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function edit_province(Request $request, $id)
    {
        $page = [
            'page_name' => 'province',
            'title' => ts('PROVINCE'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = ProvinceMaster::findOrFail($id);
        return view($this->view_edit, [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function update_province(Request $request, $id)
    {
        $model = ProvinceMaster::findOrFail($id);
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        return redirect('/admin/setting/province')->with('trash', 'sad');
    }

    public function view_region(Request $request)
    {
        $page = [
            'page_name' => 'region',
            'title' => ts('REGION'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = $this->view_list($request, 'region');
        return view($this->view_index, [
            'data'=>$data,
            'page' => $page,
        ]);
    }

    public function edit_region(Request $request, $id)
    {
        $page = [
            'page_name' => 'region',
            'title' => ts('REGION'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = RegionMaster::findOrFail($id);
        return view($this->view_edit, [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function update_region(Request $request, $id)
    {
        $model = RegionMaster::findOrFail($id);
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        return redirect('/admin/setting/region')->with('trash', 'sad');
    }*/

    public function macro_management_index(Request $request)
    {
        checkAuthority();
        $page = [
            'page_name' => 'macro_management',
            'title' => ts('STATICTEXTMANAGEMENT'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $model = new MacroManagement();
        $model = filterByColumns($model, ['macro_name', 'english_value', 'traditional_chiness_value', 'simplified_chiness_value']);
        $data = $model->orderByDesc('id')->paginate(50);
        return view('admin.admin.macro-management-index', [
            'data'=>$data,
            'page' => $page,
        ]);
    }

    public function view_macro_management(Request $request)
    {
        checkAuthority();
        $page = [
            'page_name' => 'macro_management',
            'title' => ts('STATICTEXTMANAGEMENT'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = $this->view_list($request, 'macro_management');
        return view($this->view_index, [
            'data'=>$data,
            'page' => $page,
        ]);
    }

    public function edit_macro_management(Request $request, $id)
    {
        checkAuthority();
        $page = [
            'page_name' => 'macro_management',
            'title' => ts('STATICTEXTMANAGEMENT'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = MacroManagement::findOrFail($id);
        return view('admin.setting.edit_macro_management', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function update_macro_management(Request $request, $id)
    {
        checkAuthority();
        $model = MacroManagement::findOrFail($id);
        $macro_name = str_replace(' ', '', trim(strtoupper(trim($request->input('macro_name')))));
        $macro_name = str_replace(' ', '', $macro_name);
        $model->macro_name = trim(strtoupper($model->macro_name));
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        makeLangFile('macro_management');
        return redirect('/admin/setting/macro_management')->with('trash', 'sad');
    }
    public function create_macro_management(Request $request)
    {
        checkAuthority();
        $page = [
            'page_name' => 'macro_management',
            'title' => ts('STATICTEXTMANAGEMENT'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = new MacroManagement;
        return view('admin.setting.create_macro_management', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function store_macro_management(Request $request)
    {
        checkAuthority();
        $model = new MacroManagement;
        $macro_name = str_replace(' ', '', trim(strtoupper(trim($request->input('macro_name')))));
        $macro_name = str_replace(' ', '', $macro_name);
        $model->macro_name = trim($macro_name);
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        makeLangFile('macro_management');
        return redirect('/admin/setting/macro_management')->with('trash', 'sad');
    }

    // filmtype
    public function view_filmtype(Request $request)
    {
        $page = [
            'page_name' => 'filmtype',
            'title' => ts('FILMTYPE'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $model = new FilmTypeMaster;
        $product_types = (int) $request->input('product_types');
        if ($product_types) {
            $model = $model->where('product_type_id', $product_types);
        }

        $data = $this->view_list_by_model($request, $model);
        return view('admin.setting.view_filmtype', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function edit_filmtype(Request $request, $id)
    {
        $page = [
            'page_name' => 'filmtype',
            'title' => ts('FILMTYPE'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = FilmTypeMaster::findOrFail($id);
        return view('admin.setting.edit_filmtype', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function update_filmtype(Request $request, $id)
    {
        $model = FilmTypeMaster::findOrFail($id);
        $model->product_type_id = (int) $request->input('product_type_id');
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        return redirect('/admin/setting/filmtype')->with('trash', ts('FILMTYPE') . ' ' . ts('UPDATESUCCESS'));
    }
    public function create_filmtype(Request $request)
    {
        $page = [
            'page_name' => 'filmtype',
            'title' => ts('FILMTYPE'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = new FilmTypeMaster;
        return view('admin.setting.create_filmtype', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function store_filmtype(Request $request)
    {
        $model = new FilmTypeMaster();
        $model->product_type_id = (int) $request->input('product_type_id');
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        return redirect('/admin/setting/filmtype')->with('trash', ts('FILMTYPE') . ' ' . ts('ADDSUCCESS'));
    }
    public function delete_filmtype(Request $request, $id)
    {
        $model = FilmTypeMaster::findOrFail($id);
        $model->delete();
        return ['status'=>'SUCCESS', 'msg' => ts('FILMTYPE') . ' ' . ts('DELETESUCCESS')];
    }


    // make
    public function view_make(Request $request)
    {
        $page = [
            'page_name' => 'make',
            'title' => ts('MAKE'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $model = new MakeMaster;
        $year_id = (int) $request->input('year');
        if ($year_id) {
            $model = $model->where('year_id', $year_id);
        }
        $data = $this->view_list_by_model($request, $model);
        return view('admin.setting.view_make', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function edit_make(Request $request, $id)
    {
        $page = [
            'page_name' => 'make',
            'title' => ts('MAKE'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = MakeMaster::findOrFail($id);
        return view('admin.setting.edit_make', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function update_make(Request $request, $id)
    {
        $model = MakeMaster::findOrFail($id);
        $model->year_id = (int) $request->input('year_id');
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        return redirect('/admin/setting/make')->with('trash', ts('MAKE') . ' ' . ts('UPDATESUCCESS'));
    }
    public function create_make(Request $request)
    {
        $page = [
            'page_name' => 'make',
            'title' => ts('MAKE'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = new MakeMaster;
        return view('admin.setting.create_make', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function store_make(Request $request)
    {
        $model = new MakeMaster();
        $model->year_id = (int) $request->input('year_id');
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        return redirect('/admin/setting/make')->with('trash', ts('MAKE') . ' ' . ts('ADDSUCCESS'));
    }
    public function delete_make(Request $request, $id)
    {
        $model = MakeMaster::findOrFail($id);
        $model->delete();
        return ['status'=>'SUCCESS', 'msg' => ts('MAKE') . ' ' . ts('DELETESUCCESS')];
    }

    // model
    public function view_model(Request $request)
    {
        $page = [
            'page_name' => 'model',
            'title' => ts('MODEL'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $model = new ModelMaster;
        $make_id = (int) $request->input('make_id');
        if ($make_id) {
            $model = $model->where('make_id', $make_id);
        }
        $data = $this->view_list_by_model($request, $model);
        return view('admin.setting.view_model', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function edit_model(Request $request, $id)
    {
        $page = [
            'page_name' => 'model',
            'title' => ts('MODEL'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = ModelMaster::findOrFail($id);
        return view('admin.setting.edit_model', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function update_model(Request $request, $id)
    {
        $model = ModelMaster::findOrFail($id);
        $model->make_id = (int) $request->input('make_id');
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        return redirect('/admin/setting/model')->with('trash', ts('MODEL') . ' ' . ts('UPDATESUCCESS'));
    }
    public function create_model(Request $request)
    {
        $page = [
            'page_name' => 'model',
            'title' => ts('MODEL'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = new ModelMaster;
        return view('admin.setting.create_model', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function store_model(Request $request)
    {
        $model = new ModelMaster();
        $model->make_id = (int) $request->input('make_id');
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        return redirect('/admin/setting/model')->with('trash', ts('MODEL') . ' ' . ts('ADDSUCCESS'));
    }
    public function delete_model(Request $request, $id)
    {
        $model = ModelMaster::findOrFail($id);
        $model->delete();
        return ['status'=>'SUCCESS', 'msg' => ts('MODEL') . ' ' . ts('DELETESUCCESS')];
    }

    // part
    public function view_part(Request $request)
    {
        $page = [
            'page_name' => 'part',
            'title' => ts('PART'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $model = new PartMaster;
        $year_id = (int) $request->input('year');
        if ($year_id) {
            $model = $model->where('year_id', $year_id);
        }
        $data = $this->view_list_by_model($request, $model);
        return view('admin.setting.view_part', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function edit_part(Request $request, $id)
    {
        $page = [
            'page_name' => 'part',
            'title' => ts('PART'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = PartMaster::findOrFail($id);
        return view('admin.setting.edit_part', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function update_part(Request $request, $id)
    {
        $model = PartMaster::findOrFail($id);
        /*$model->year_id = (int) $request->input('year_id');*/
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->min_length = (int) $request->input('min_length');
        $model->save();
        $url = $request->input('_previous_') ?: '/admin/setting/part';
        return redirect($url)->with('trash', ts('PART') . ' ' . ts('UPDATESUCCESS'));
    }
    public function create_part(Request $request)
    {
        $page = [
            'page_name' => 'part',
            'title' => ts('PART'),
            'breadcrumb' => [
                [
                    'text' => ts('DATADICTIONARY'),
                    'url' => 'javascript:void(0)'
                ]
            ],
        ];
        $data = new PartMaster;
        return view('admin.setting.create_part', [
            'data'=>$data,
            'page' => $page,
        ]);
    }
    public function store_part(Request $request)
    {
        $model = new PartMaster();
        /*$model->year_id = (int) $request->input('year_id');*/
        $model->english_value = trim($request->input('english_value'));
        $model->traditional_chiness_value = trim($request->input('traditional_chiness_value'));
        $model->simplified_chiness_value = trim($request->input('simplified_chiness_value'));
        $model->save();
        return redirect('/admin/setting/part')->with('trash', ts('PART') . ' ' . ts('ADDSUCCESS'));
    }
    public function delete_part(Request $request, $id)
    {
        $model = PartMaster::findOrFail($id);
        $model->delete();
        return ['status'=>'SUCCESS', 'msg' => ts('PART') . ' ' . ts('DELETESUCCESS')];
    }


    public function view_list(Request $request, $model)
    {
        if ($model == 'cities') {
            $model = new Cities();
        } elseif ($model == 'province') {
            $model = new ProvinceMaster();
        } elseif ($model == 'region') {
            $model = new RegionMaster();
        } elseif ($model == 'macro_management') {
            $model = new MacroManagement();
        } else {
            return false;
        }

        $label = $request->input('label');
        $value = $request->input('value');
        if (!empty($value)) {
            if ($label == 1) {
                $model = $model->where('english_value', 'like', '%'.$value.'%');
            } elseif ($label == 2) {
                $model = $model->where('traditional_chiness_value', 'like', '%'.$value.'%');
            } elseif ($label == 2) {
                $model = $model->where('simplified_chiness_value', 'like', '%'.$value.'%');
            } else {
                $model = $model->orWhere('english_value', 'like', '%'.$value.'%')
                    ->orWhere('traditional_chiness_value', 'like', '%'.$value.'%')
                    ->orWhere('simplified_chiness_value', 'like', '%'.$value.'%');
            }
        }
        $data = $model->orderByDesc('id')->paginate(50);
        return $data;
    }

    public function view_list_by_model(Request $request, $model)
    {
        $label = $request->input('label');
        $value = $request->input('value');
        if (!empty($value)) {
            $model = filterByColumns($model, ['english_value', 'traditional_chiness_value', 'simplified_chiness_value']);
            /*if ($label == 1) {
                $model = $model->where('english_value', 'like', '%'.$value.'%');
            } elseif ($label == 2) {
                $model = $model->where('traditional_chiness_value', 'like', '%'.$value.'%');
            } elseif ($label == 2) {
                $model = $model->where('simplified_chiness_value', 'like', '%'.$value.'%');
            } else {
                $model = $model->orWhere('english_value', 'like', '%'.$value.'%')
                    ->orWhere('traditional_chiness_value', 'like', '%'.$value.'%')
                    ->orWhere('simplified_chiness_value', 'like', '%'.$value.'%');
            }*/
        }

        $data = $model->orderBy('id')->paginate(50);
        return $data;
    }
}
